#include "Bolt.h"

RailBolt::RailBolt()
{
	BoundingBox = NULL;
	LaunchSpeed = 0;
}

RailBolt::RailBolt(IDirect3DDevice9 *nDevice,  float nLaunchSpeed, D3DXVECTOR3 nAngles) : BaseIndexedGraphicalEntity(nDevice)
{
	Position = D3DXVECTOR3(0,0,0);
	Velocity = D3DXVECTOR3(0,0,0);
	LaunchTime = 0;
	IsLaunched = false;
	IsActive = false;
	Angles = nAngles;
	LaunchSpeed = nLaunchSpeed;
	BoundingBox = new PhysicsBox(nDevice);
	CreateCube(D3DXVECTOR3(.2f, .2f, 2.f));
	BoundingBox->InitCollisionBox(.2f,.2f,2.f);
}

RailBolt::~RailBolt()
{
	if(BoundingBox)
		delete BoundingBox;
}

void RailBolt::Update(float dt)
{
	if(IsActive)
	{
		D3DXVECTOR3 TravelVector;
		D3DXMATRIX RotationMatrix;

		if(IsLaunched)
		{
			TravelVector = D3DXVECTOR3(0,0,1);
			D3DXMatrixRotationYawPitchRoll(&RotationMatrix, Angles.x, Angles.y, Angles.z);
			D3DXVec3TransformCoord(&TravelVector, &TravelVector, &RotationMatrix);
			Velocity = 	(TravelVector * LaunchSpeed);
		}
		if(BoundingBox)
		{
			BoundingBox->SetPosition(Position);
			BoundingBox->SetAngles(Angles);
			BoundingBox->Update(dt);
		}
	}
	else
	{
		Velocity *= 0;
	}
	BaseIndexedGraphicalEntity::Update(dt);
}

void RailBolt::Render( void )
{
	if(IsLaunched)
	{
		BaseIndexedGraphicalEntity::Render();
		if(BoundingBox)
			BoundingBox->Render();
	}
}

void RailBolt::Launch( float Time )
{
	LaunchTime = Time;
	IsLaunched = true;
	IsActive = true;
}

bool RailBolt::GetIsLaunched( void )
{
	return IsLaunched;
}

void RailBolt::SetIsActive( bool nIsActive )
{
	IsActive = nIsActive;
}

D3DXVECTOR3 RailBolt::GetAngles( void )
{
	return Angles;
}

float RailBolt::GetLaunchTime( void )
{
	return LaunchTime;
}

float RailBolt::GetLaunchSpeed( void )
{
	return LaunchSpeed;
}

void RailBolt::SetAngles(D3DXVECTOR3 nAngles)
{
	Angles = nAngles;
}

bool RailBolt::GetIsActive( void )
{
	return IsActive;
}